package com.zq.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import com.zq.pojo.MsgInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ConsumerService {
    @Autowired
    private ObjectMapper objectMapper;
    private static final Logger logger= LoggerFactory.getLogger(ConsumerService.class);

    @RabbitListener(queues = {"autoQueue"})
    public void recive(@Payload String string, Channel channel, Message message) throws IOException {
        try {
            MsgInfo msgInfo = objectMapper.readValue(string, MsgInfo.class);
            Thread.sleep(20000);
            int i=10/0;
            System.out.println("消费成功："+msgInfo);
            logger.info("消费成功："+msgInfo);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            System.out.println("消费失败：");
            logger.info("消费失败");
        }

    }
}
